#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"
#include "root.h"
#include <QFile>
#include <QDebug>
#include <QMessageBox>
#include <QStringList>
#include "id.h"
#include <vector>
#include "teacher.h"
#include "student.h"
#include "paper.h"
#include "program.h"
using namespace std;
id u;
int l=0;
vector <Teacher*> tdata;
vector <student*> sdata;
vector <paper*> pdata;
vector <Program*> prdata;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{   if(l==0){
    qDebug()<<"no";
    QString s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12;
    QFile tfile("C:/sr_system/Zteacher.txt");
    tfile.open(QIODevice::ReadOnly);
    while(!tfile.atEnd()){
        s1=tfile.readLine();
        s1=s1.trimmed();
        s2=tfile.readLine();
        s2=s2.trimmed();
        s3=tfile.readLine();
        s3=s3.trimmed();
        s4=tfile.readLine();
        s4=s4.trimmed();
        s5=tfile.readLine();
        s5=s5.trimmed();
        s6=tfile.readLine();
        s6=s6.trimmed();
        s7=tfile.readLine();
        s7=s7.trimmed();
        s8=tfile.readLine();
        s8=s8.trimmed();
        s9=tfile.readLine();
        s9=s9.trimmed();
        s10=tfile.readLine();
        s10=s10.trimmed();
        if(s1==""){
            break;
        }
        tdata.push_back(new Teacher(s1,s2,s3.toInt(),s4.toInt(),s5,s6,s7,s8,s9.toInt(),s10.toInt()));
    }
    tfile.close();
    QFile sfile("C:/sr_system/Zstudent.txt");
    sfile.open(QIODevice::ReadOnly);
    while(!sfile.atEnd()){
        s1=sfile.readLine();
        s1=s1.trimmed();
        s2=sfile.readLine();
        s2=s2.trimmed();
        s3=sfile.readLine();
        s3=s3.trimmed();
        s4=sfile.readLine();
        s4=s4.trimmed();
        s5=sfile.readLine();
        s5=s5.trimmed();
        s6=sfile.readLine();
        s6=s6.trimmed();
        s7=sfile.readLine();
        s7=s7.trimmed();
        s8=sfile.readLine();
        s8=s8.trimmed();
        s9=sfile.readLine();
        s9=s9.trimmed();
        s10=sfile.readLine();
        s10=s10.trimmed();
        if(s1==""){
            break;
        }
        sdata.push_back(new student(s1,s2,s3.toInt(),s4.toInt(),s5,s6,s7,s8,s9.toInt(),s10.toInt()));
    }
    sfile.close();
    QFile prfile("C:/sr_system/Zprogram.txt");
    prfile.open(QIODevice::ReadOnly);
    while(!prfile.atEnd()){
        vector <QString>name;
        s1=prfile.readLine();
        s1=s1.trimmed();
        s2=prfile.readLine();
        s2=s2.trimmed();
        s3=prfile.readLine();
        s3=s3.trimmed();
        s4=prfile.readLine();
        s4=s4.trimmed();
        s5=prfile.readLine();
        s5=s5.trimmed();
        s6=prfile.readLine();
        s6=s6.trimmed();
        s7=prfile.readLine();
        s7=s7.trimmed();
        s8=prfile.readLine();
        s8=s8.trimmed();
        s9=prfile.readLine();
        s9=s9.trimmed();
        s10=prfile.readLine();
        s10=s10.trimmed();
        s11=prfile.readLine();
        s11=s11.trimmed();
        s12=prfile.readLine();
        s12=s12.trimmed();
        if(s1==""){
            break;
        }
        QStringList list=s8.split(",");
        for(int i=0;i<list.size();i++){
            name.push_back(list[i]);
        }
        prdata.push_back(new Program(s1,s2,s3,s4,s5,s6,s7.toFloat(),name,s9.toInt(),s10.toFloat(),s11,s12.toInt()));
    }
    prfile.close();
    QFile pfile("C:/sr_system/Zpaper.txt");
    pfile.open(QIODevice::ReadOnly);
    while(!pfile.atEnd()){
        vector <QString> name;
        s1=pfile.readLine();
        s1=s1.trimmed();
        s2=pfile.readLine();
        s2=s2.trimmed();
        s3=pfile.readLine();
        s3=s3.trimmed();
        s4=pfile.readLine();
        s4=s4.trimmed();
        s5=pfile.readLine();
        s5=s5.trimmed();
        s6=pfile.readLine();
        s6=s6.trimmed();
        s7=pfile.readLine();
        s7=s7.trimmed();
        s8=pfile.readLine();
        s8=s8.trimmed();
        s9=pfile.readLine();
        s9=s9.trimmed();
        s10=pfile.readLine();
        s10=s10.trimmed();
        if(s1==""){
            break;
        }
        QStringList list=s1.split(",");
        for(int i=0;i<list.size();i++){
            name.push_back(list[i]);
        }
        pdata.push_back(new paper(name,s2,s3,s4,s5,s6.toInt(),s7.toInt(),s8.toInt(),s9.toInt(),s10.toInt()));
    }
    pfile.close();
    l=1;}
    ui->setupUi(this);
}
MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_3_clicked()
{
    int r=0,i=0;
    u.str1=ui->lineEdit->text();
    u.str2=ui->lineEdit_2->text();
    QFile file("C:/sr_system/Zload.txt");
    file.open(QIODevice::ReadOnly);
    QByteArray Id,Passward;
    while(!file.atEnd()){
        Id=file.readLine();
        Id=Id.trimmed();
        Passward=file.readLine();
        Passward=Passward.trimmed();
        if(i==0&&u.str1==Id&&u.str2==Passward){
            file.close();
            r=1;
            this->hide();
            root* p=new(root);
            p->setModal(true);
            p->show();
            QMessageBox::information(this,"欢迎","Root 你好");
        }
        else if(u.str1==Id&&u.str2==Passward){
            file.close();
            r=1;
            this->hide();
            Dialog *dialog=new(Dialog);
            dialog->setModal(true);
            dialog->show();
            for(i=0;(unsigned int)i<tdata.size();i++){
                if(u.str1==tdata[i]->findnumber())
            QMessageBox::information(this,"欢迎",tdata[i]->findname()+" 你好");
            }
            for(i=0;(unsigned int)i<sdata.size();i++){
                if(u.str1==sdata[i]->findnumber())
            QMessageBox::information(this,"欢迎",sdata[i]->findname()+" 你好");
            }
        }
        i=1;
    }
    if(r==0){
    file.close();
    this->close();
    QMessageBox::critical(this,"错误","用户名或密码错误");
    MainWindow*mainwindow=new(MainWindow);
    mainwindow->show();
    }
}

void MainWindow::on_pushButton_4_clicked()
{
    this->close();
}
void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
        QPixmap pix;
        pix.load(":/timg.jpg");
        pix = pix.scaled(pix.width()*0.1,pix.height()*0.1);
        painter.drawPixmap(125,120,pix);
}
